package com.example.moveclock.views;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Picture;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PictureDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.example.moveclock.activities.R;

public class AngryFaggotsView extends SurfaceView implements
		SurfaceHolder.Callback {
	private FaggotsThread thread;

	public AngryFaggotsView(Context context, AttributeSet attrs) {
		super(context, attrs);

		// register our interest in hearing about changes to our surface
		SurfaceHolder holder = getHolder();
		holder.addCallback(this);

		// create thread only; it's started in surfaceCreated()
		thread = new FaggotsThread(holder, context, new Handler() {
			@Override
			public void handleMessage(Message m) {
			}
		});

		setFocusable(true); // make sure we get key events
	}

	public class FaggotsThread extends Thread {

		private boolean mRun;
		private SurfaceHolder mSurfaceHolder;
		private Bitmap mBackgroundImage;
		private Resources res;
		private Drawable mFaggot;
		private int i;
		private boolean draw;

		/** The thread that actually draws the animation */

		public FaggotsThread(SurfaceHolder surfaceHolder, Context context,
				Handler handler) {
			mSurfaceHolder = surfaceHolder;
			res = context.getResources();
			mBackgroundImage = BitmapFactory.decodeResource(res,
					R.drawable.purplelines);

			mFaggot = res.getDrawable(R.drawable.face);
		}

		@Override
		public void run() {
			while (mRun) {
				Canvas c = null;
				try {
					c = mSurfaceHolder.lockCanvas(null);
					synchronized (mSurfaceHolder) {
						doDraw(c);
					}
				} finally {
					// do this in a finally so that if an exception is thrown
					// during the above, we don't leave the Surface in an
					// inconsistent state
					if (c != null) {
						mSurfaceHolder.unlockCanvasAndPost(c);
					}
				}
			}
		}

		private void doDraw(Canvas canvas) {
			canvas.drawBitmap(mBackgroundImage, 0, 0, null);

			canvas.save();

			int j = getRandomPoint();
			mFaggot.setBounds(j, j, j + mFaggot.getIntrinsicWidth(), j
					+ mFaggot.getIntrinsicHeight());
			mFaggot.draw(canvas);

			canvas.restore();

		}

		private int getRandomPoint() {
			return (int) (Math.random() * 50 + 1);
		}

		public void doStart() {
			synchronized (mSurfaceHolder) {
				// First set the game for Medium difficulty

			}
		}

		public void setRunning(boolean b) {
			mRun = b;
		}

	}

	public AngryFaggotsView(Context context) {
		super(context);

	}

	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub

	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub

	}

	public FaggotsThread getThread() {
		return thread;
	}

	/*
	 * Callback invoked when the Surface has been created and is ready to be
	 * used.
	 */
	public void surfaceCreated(SurfaceHolder holder) {
		// start the thread here so that we don't busy-wait in run()
		// waiting for the surface to be created
		thread.setRunning(true);
		thread.start();
	}

}
